<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <link rel="stylesheet" href="https://at.alicdn.com/t/font_882189_utrw7bdcle9.css">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>海草新版本发布</title>
  <style>
    .__Tips {
  top: 15px;
  left: 50%;
  transform: translateX(-50%);
  /* min-width: 150px; */
  height: 35px;
  z-index: 9999999999;
  position: fixed;
  line-height: 35px;
  text-align: center;
  background: #fff;
  padding: 0 20px 0 15px;
  border-radius: 4px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
  transition: all 0.3s;
  white-space: nowrap;
  overflow: hidden;
  top: -40px;
}
.__Tips[type="error"]::before{
  content: "\e603";
  font-family: "iconfont" !important;
  -webkit-font-smoothing: antialiased;
  font-style: normal;
  padding-right: 5px;font-size: 14px;
  vertical-align: middle;
  color:red;
}

.__Tips[type="loading"]::before{
  content: "\e61d";
  font-family: "iconfont" !important;
  -webkit-font-smoothing: antialiased;
  font-style: normal;
  padding-right: 5px;font-size: 14px;
  vertical-align: middle;
  color:#95cdfb;
  display: inline-block;
  animation: xuanzhuan 0.5s linear infinite;
  width:15px;height:15px;text-align:center;line-height: 15px;
  transform-origin: center;
}

@keyframes xuanzhuan {
  from{
    transform: rotate(0deg)
  }
  to{
    transform: rotate(360deg)
  }
}
/* .__Tips[type="error"]::before{
  content: "\e603";
  font-family: "iconfont" !important;
  -webkit-font-smoothing: antialiased;
  font-style: normal;
  padding-right: 5px;font-size: 14px;
  vertical-align: middle;
  color:red;
} */
  </style>
  <style>
    .main {
      width: 500px;
      height: auto;
      padding: 10px;
      margin: 100px auto;
      background: #fff;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
    }
    .uploadbox{
      display: flex;
    }
    .uploadbtn {
      height: 50px;
      flex: 1;
      line-height: 50px;
      background: #98c36e;
      border-radius: 2px;
      text-align: center;
      font-size: 20px;
      color: #fff;
      cursor: pointer;
      margin: 10px;
      user-select: none;
      position: relative;
    }
    .uploadbtn .uploadimg{
      position: absolute;
      top:0;left:0;background:#bac7ad;
      color:#000;
      font-size: 20px;
      border-radius: 2px;
      width:100%;
      height:100%;
      display: none;
    }
    .uploadbtn .link{
      position: absolute;
      display: none;
      bottom: -20px;
      line-height: 20px;
      color:#2b2bff;
      text-decoration: underline;width:100%;
      text-align: center;
      left:0;
      font-size: 14px;
    }

    .uploadbtn:active {
      background: #7ba74f;
    }
    .uploadbtn[loading="true"] {
      background: #7ba74f;
    }
    .uploadbtn[loading="true"] .uploadimg{
      display: block;
    }

    .filename {
      height: 40px;
      margin: 0;
      line-height: 40px;
    }

    .formtijiao {
      height: 50px;
      line-height: 50px;
      background: #e06253;
      border-radius: 2px;
      text-align: center;
      font-size: 20px;
      color: #fff;
      cursor: pointer;
      user-select: none;
    }

    .formtijiao:active {
      background: #da5647;
    }
    .tit{
      font-size: 12px;color:#999;
    }
    input{
      width:calc(100% - 12px);
      border:1px solid #999999;
      border-radius: 2px;
      line-height: 40px;
      padding:0 5px;
      margin:5px 0;
      outline: none;
      color:#666;
    }
    textarea{
      width:calc(100% - 12px);
      border:1px solid #999999;
      border-radius: 2px;
      padding:5px;
      line-height: 20px;
      outline: none;
      margin:5px 0;
      color:#666;
      height:120px;
    }
    .radio{
      display: inline-block;margin-right: 20px;cursor: pointer;margin-bottom: 5px;
      color:#666;font-size: 12px;line-height: 14px;
    }
    .radio input{
      width:14px;height:14px;cursor: pointer;vertical-align: top;position: relative;
      top:-4px;
    }
  </style>
</head>

<body>
  <div class="main">
    <div class="tit">版本号</div>
    <input type="text" id="version" placeholder="版本号">
    <div class="tit">发布客户端</div>
    <div id="types">
        <label class="radio" onclick="setVersionType(1002)"><input checked type="radio" name="types" value="1002">PC客户端</label>
        <label class="radio" onclick="setVersionType(1005)"><input type="radio" name="types" value="1005">Android</label>
        <label class="radio" onclick="setVersionType(1006)"><input type="radio" name="types" value="1006">iOS</label>
    </div>
    <div class="uploadbox">
      <div class="uploadbtn" type="ia32">
        32位安装包
        <div class="uploadimg">上传中</div>
        <a href="" class="link">下载链接</a>
      </div>
      <div class="uploadbtn" type="x64">
        64位安装包
        <div class="uploadimg">上传中</div>
        <a href="" class="link">下载链接</a>
      </div>
    </div>
    <p class="filename"></p>
    <div class="tit">添加功能</div>
    <textarea id="addFun" placeholder="添加的功能，一行一个"></textarea>
    <div class="tit">修复BUG</div>
    <textarea id="deBug" placeholder="修复的BUG，一行一个"></textarea>
    <div class="formtijiao">提交</div>
    <p class="links"></p>
  </div>
  <script>
      function Tips (data) {
        this.msg = data.msg
        this.type = data.type
        if (!this.elbox) {
          this.render()
        }
        return this
      }
      Tips.prototype.show = function () {
        this.elbox.style.top = '15px'
      }
      Tips.prototype.hide = function () {
        this.elbox.style.top = '-60px'
      }
      Tips.prototype.render = function () {
        let body = document.getElementsByTagName('body')[0]
        if (this.elbox) {
          this.elbox.setAttribute('type', this.type)
          this.elbox.innerHTML = this.msg
        } else {
          this.elbox = document.createElement('div')
          this.elbox.className = '__Tips'
          this.elbox.setAttribute('type', this.type)
          this.elbox.innerHTML = this.msg
          body.appendChild(this.elbox)
        }
      }
      </script>
  <script>
    var serverType = '{{type}}'
    var selectFile,
    installlist = {},
    versionType = 1002;
    var uploadErr = new Tips({
      type: 'error',
      msg: '上传失败请重试'
    })
    var errTips = new Tips({
      type: 'error',
      msg: '发布失败，请重试！'
    })
    var loadTips = new Tips({
      type: 'loading',
      msg: '上传中，请稍候！'
    })
    var successTips = new Tips({
      type: 'success',
      msg: '发布成功！'
    })
    $( function() {
      $( '.uploadbtn' ).click( function() {
        if ($(this).attr('loading') === 'true') return
        let inputFile = document.createElement( 'input' )
        inputFile.setAttribute( 'type', 'file' )
        var _this = this
        inputFile.onchange = function() {
          var file = this.files[0]
          var objname = `${serverType}/${Date.now()}_${serverType}_${versionType}_${$(_this).attr('type')}.exe`
          if (file.type !== 'application/x-msdownload') {
            return alert('请选择正确的安装包格式！')
          }
          let formdata = new FormData()
          formdata.append('file', file)
          formdata.append('objname', objname)
          $(_this).attr('loading', 'true')
          $.ajax({
            url: '/uploadOss',
            type: 'POST',
            cache: false,
            data: formdata,
            processData: false,
            contentType: false,
            xhr: function(){ 
              myXhr = $.ajaxSettings.xhr(); 
              if(myXhr.upload){ // check if upload property exists 
                myXhr.upload.addEventListener('progress',function(e){ 
                  var loaded = e.loaded;//已经上传大小情况
                  var tot = e.total;//附件总大小
                  var per = Math.floor(100*loaded/tot);  //已经上传的百分比
                  console.log(per + '%');
                }, false); // for handling the progress of the upload
              }  
              return myXhr; 
            },
            success: function(res) {
              $(_this).attr('loading', '')
              if (res.status !== 1) {
                uploadErr.show()
                setTimeout(() => {
                  uploadErr.hide()
                }, 800)
                return
              }
              console.log(window.location.origin + res.url)
              $(_this).find('.link').attr('href', res.url)
              $(_this).find('.link').show()
              installlist[$(_this).attr('type')] = objname
            },
            error: function (res) {
              uploadErr.show()
              setTimeout(() => {
                uploadErr.hide()
              }, 800)
            }
          })
        }
        inputFile.click()
      } )

      $( '.uploadbtn .link' ).click(function (event) {
        event.stopPropagation();
      })
      $('.formtijiao').click(function() {
        var version = $('#version').val();
        var addFun = $('#addFun').val();
        var deBug = $('#deBug').val();
        var files = installlist
        if (Object.keys(files).length === 0) {
          alert('请上传安装包')
          return
        }
        if (version === '') {
          alert('请填写版本号')
          return
        }
        if (!(addFun || deBug)) {
          alert('添加功能和修复 必须填写一项')
          return
        }
        $.post('/uploadversion', {
          type: versionType,
          version: version,
          files: JSON.stringify(files),
          addfun: addFun,
          debug: deBug
        }, function (res) {
          if (res.status !== 1) {
            errTips.show()
            setTimeout(() => {
              errTips.hide()
            }, 800)
            return;
          }
          successTips.show()
          window.location.href = "/getVersion?type=" + versionType
        })

      })
    })

    function setVersionType (type) {
      versionType = type
    }
  </script>


</body>

</html>